﻿using NCP.Core.DataUtil;
using NCP.Core.IoC;
using NCP.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
namespace NCP.Service
{
   public class LimitTimeService : IService
    {
        private SqlSugarClient _client;
        public LimitTimeService()
        {
            _client = DbHelper.GetSqlSugarClient();
        }
        //获取未限制药品基本信息
        public List<PhaBaseInfo> GetunLimitDrugInfo()
        {
            var result = _client.SqlQueryable<PhaBaseInfo>(@"select * from PhaBaseInfo o 
                        where o.ValidFlag='1' 
                        and o.DrugCode not in (select n.DrugCode from LimitTime n )
                        ").ToList();
            return result;
        }
        //获取已限制药品基本信息
        public List<LimitTimeNew> GetLimitDrugInfo()
        {
            var result = _client.SqlQueryable<LimitTimeNew>(@" select  e.DrugCode ,o.DrugName ,convert(char(10),e.LimitDate,126) LimitDate,e.Days,o.Specs,o.UnitPrice,o.SpellCode,o.ProducerName
  from LimitTime  e
  left join PhaBaseInfo o on e.DrugCode=o.DrugCode
").ToList();
            return result;
        }

        public int SaveLimiteTime(List<LimitTimeNew> items)
        {
            var oldItems = _client.Queryable<LimitTime>().ToList();

            _client.BeginTran();
            try
            {
                #region 操作日志

                string oldCon = "";
                string newCon = "";
                oldItems.ForEach(x => oldCon += $"{x.DrugCode},{x.DrugName},{x.LimitDate},{x.Days}^");
                if (items != null)
                {
                    items.ForEach(x => newCon += $"{x.DrugCode},{x.DrugName},{x.LimitDate},{x.Days}^");
                }
                SysOperLogService.WriteSysOperLog(new SysOperLog
                {
                    EventType = "修改药品限制时间信息",
                    EventCon = "修改",
                    OldCon = oldCon.TrimEnd('^'),
                    NewCon = newCon.TrimEnd('^')
                });

                #endregion
                //删除信息
                // List<string> delItems = new List<string>();
                _client.Deleteable<LimitTime>().ExecuteCommand();

                //保存信息
                var insertItems = new List<LimitTime>();
                if (items != null)
                {
                    foreach (var item in items)
                    {
                        LimitTime rr = new LimitTime();
                        rr.DrugCode = item.DrugCode;
                        rr.DrugName = item.DrugName;
                        rr.LimitDate = Convert.ToDateTime( item.LimitDate);
                        rr.Days = item.Days;
                        insertItems.Add(rr);
                    }
                    _client.Insertable(insertItems).ExecuteCommand();
                }
                
                _client.CommitTran();
            }
            catch
            {
                _client.RollbackTran();
                throw;
            }
            return 1;
        }
    }
}
